﻿using System;
using System.IO;
using RayDen.Library.Components.Color.Types;
using RayDen.Library.Core.Primitives;
using RayDen.RayEngine.Core.Types;
using RayDen.RayEngine.Corrector.Types;

namespace RayDen.RayEngine.Corrector.Renderers
{
    public class PathSampler
    {
        public PathSampleProcessor Processor;
        public FrameConfiguration FrameConfig;
        public IColorType Radiance, Throughput;
        protected internal int RayIndex;
        protected internal RayData PathRay;
        protected int State, Depth;
        protected float PathWeight;
        protected bool SpecularBounce;
        public UInt64 PathExpression;

        protected ImagePlaneSample imageSample;

        public virtual void InitPath(PathSampleProcessor renderer)
        {
            this.Processor = renderer;
            imageSample = renderer.GetImageSample();
        }

        public virtual bool FillRayBuffer(RayBuffer rayBuffer)
        {
            var leftSpace = rayBuffer.LeftSpace();
            if (leftSpace < 1)
                return false;

            RayIndex = rayBuffer.AddRay(ref PathRay);

            return true;
        }

        public virtual void Advance(RayBuffer rayBuffer, SampleBuffer<IColorType> consumer)
        {
            Splat(consumer);
            InitPath(this.Processor);
        }

        public virtual void Splat(SampleBuffer<IColorType> sampleBuffer)
        {
            Radiance = new RgbColor(Processor.samplers.PrimarySpaceSampler.GetSample());
            sampleBuffer.SplatSample(imageSample.X, imageSample.Y, ref Radiance);
        }
    }

}